{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 把dir_frames里的所有*.png转换成avi视频"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|████████████████████████████████████████████████████████████████████████████████| 900/900 [00:25<00:00, 35.33it/s]\n"
     ]
    }
   ],
   "source": [
    "import cv2\n",
    "from tqdm import tqdm\n",
    "img_root = 'dir_frames'             #这里写你的文件夹路径，比如：/home/youname/data/img/,注意最后一个文件夹要有斜杠\n",
    "fps = 24                            #保存视频的FPS，可以适当调整\n",
    "frame_size=(1600,900)\n",
    "img_path = gb.glob(\"dir_frames/*.png\") #获取的路径列表是从低到高排列的\n",
    "fourcc = cv2.VideoWriter_fourcc(*'XVID')#可以用(*'DVIX')或(*'X264'),如果都不行先装ffmepg: sudo apt-get install ffmepg\n",
    "videoWriter = cv2.VideoWriter('video.avi',fourcc,fps,frame_size)#最后一个是保存图片的尺寸\n",
    "for path in tqdm(img_path):\n",
    "    frame = cv2.imread(path)\n",
    "    videoWriter.write(frame)\n",
    "videoWriter.release()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 把dir_frames里的所有*.png转换成mp4视频-fixme运行不成功"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|████████████████████████████████████████████████████████████████████████████████| 900/900 [00:23<00:00, 37.55it/s]\n"
     ]
    }
   ],
   "source": [
    "# encoding: UTF-8\n",
    "import glob as gb\n",
    "import cv2\n",
    "fps = 24\n",
    "frame_size=(1600,900)\n",
    "img_path = gb.glob(\"dir_frames/*.png\") \n",
    "videoWriter = cv2.VideoWriter('video.mp4', cv2.VideoWriter_fourcc(*'X264'), fps, frame_size)\n",
    "for path in tqdm(img_path):\n",
    "    img  = cv2.imread(path)\n",
    "    videoWriter.write(img)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 这个版本含有时间轴自动对齐，补帧"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('min #: ', 10000001027L)\n",
      "('max #: ', 10000004231L)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 101/101 [00:07<00:00, 13.17it/s]\n"
     ]
    }
   ],
   "source": [
    "import cv2\n",
    "import glob as gb\n",
    "from tqdm import tqdm\n",
    "\n",
    "fig_dir_path = \"dir_frames_breath\"\n",
    "video_file_name = 'breath_v6.avi'\n",
    "img_paths = gb.glob(fig_dir_path+\"/*.png\")\n",
    "img_paths = [int(p.replace(fig_dir_path,\"\").replace(\"\\\\\",\"\").replace(\".png\",\"\"))for p in img_paths]\n",
    "img_paths.sort()\n",
    "\n",
    "millis_min = img_paths[0]\n",
    "millis_max = img_paths[-1]\n",
    "print(\"min #: \", millis_min)\n",
    "print(\"max #: \", millis_max)\n",
    "\n",
    "frame_size = (2560, 1440)\n",
    "fourcc = cv2.VideoWriter_fourcc(*'XVID')  # 可以用(*'DVIX')或(*'X264'),如果都不行先装ffmepg: sudo apt-get install ffmepg\n",
    "videoWriter = cv2.VideoWriter(video_file_name, fourcc, 32, frame_size)  # 最后一个是保存图片的尺寸\n",
    "\n",
    "# last_f存储上一次选中的帧,他的作用在于，如果没有找到合适的帧，就用上一次有效的帧来替代\n",
    "last_f = millis_min\n",
    "\n",
    "for f in tqdm(range(millis_min,millis_max,32)):\n",
    "    has_flag = False\n",
    "    has_f = f\n",
    "    for k in range(0, 17):\n",
    "        cur_f = f + k\n",
    "        if cur_f in img_paths:\n",
    "            has_flag = True\n",
    "            has_f = cur_f\n",
    "            break\n",
    "        cur_f = f - k\n",
    "        if cur_f in img_paths:\n",
    "            has_flag = True\n",
    "            has_f = cur_f\n",
    "            break\n",
    "\n",
    "    if has_flag == False:\n",
    "        has_f = last_f; # 如果没有找到目标帧，则使用前一帧补偿\n",
    "        print(\"An patch frame happens \"+str(k)+\"->\"+str(has_f))\n",
    "    else:\n",
    "        last_f = has_f; # 如果找到了目标真，那么就用找到的这一帧代替\n",
    "    \n",
    "    if has_f in img_paths:\n",
    "        path = fig_dir_path+\"/\"+str(has_f)+\".png\"\n",
    "        frame = cv2.imread(path)\n",
    "        videoWriter.write(frame)\n",
    "    else:\n",
    "        print(\"vital error\")\n",
    "        break\n",
    "videoWriter.release()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
